package com.huluobo.animdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView iv = findViewById(R.id.iv);
        ImageView progress_iv = findViewById(R.id.iv2);
        Button btn_tran = findViewById(R.id.trans_btn);
        Button btn_rotate = findViewById(R.id.rotates_btn);
        Button btn_scale = findViewById(R.id.scale_btn);
        Button btn_alpha = findViewById(R.id.alpha_btn);
        Button btn_set = findViewById(R.id.set_btn);
        Button btn_progress = findViewById(R.id.progress_btn);
        Button btn_value = findViewById(R.id.value_btn);
        Button btn_object = findViewById(R.id.object_btn);
        Button btn_inter = findViewById(R.id.interpolator_btn);

        //平移动画
        Animation animationTranslate = AnimationUtils.loadAnimation(this, R.anim.anim_tran);
        btn_tran.setOnClickListener(v -> {
            iv.startAnimation(animationTranslate);
        });

        //旋转动画
        Animation animationRotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
        btn_rotate.setOnClickListener(v -> {
            iv.startAnimation(animationRotate);
        });

        //缩放动画
        Animation animationScale = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
        btn_scale.setOnClickListener(v -> {
            iv.startAnimation(animationScale);
        });

        //透明度动画
        Animation animationAlpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
        btn_alpha.setOnClickListener(v -> {
            iv.startAnimation(animationAlpha);
        });

        //动画集合
        Animation animationSet = AnimationUtils.loadAnimation(this, R.anim.anim_set);
        btn_set.setOnClickListener(v -> {
            iv.startAnimation(animationSet);
        });

        AnimationDrawable animationDrawable = (AnimationDrawable) progress_iv.getDrawable();
        btn_progress.setOnClickListener(v -> {
            animationDrawable.start();
        });

        btn_value.setOnClickListener(v -> {
            ValueAnimator valueAnimator = ValueAnimator.ofFloat(1f, 0f);
            valueAnimator.setDuration(5000);
            valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
            valueAnimator.setRepeatMode(ValueAnimator.REVERSE);

            valueAnimator.addUpdateListener(animation -> {
                float currentValue = (float) animation.getAnimatedValue();
                Log.i("TagA", "currentValue = " + currentValue);
                iv.setScaleX(currentValue);
                iv.setScaleY(currentValue);
                iv.setAlpha(currentValue);
            });
            valueAnimator.start();
        });

        btn_object.setOnClickListener(v -> {
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv, "scaleX", 0, 3);
            objectAnimator.setDuration(5000);
            objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
            objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
            objectAnimator.start();
        });

        //插值器
        btn_inter.setOnClickListener(v -> {
            ObjectAnimator translateY = ObjectAnimator.ofFloat(iv, "translationY", 50, 700);

            //添加插值器
//            translateY.setInterpolator(new AccelerateInterpolator());//加速插值器,开始慢,慢慢加速,中间快
//            translateY.setInterpolator(new DecelerateInterpolator());//减速插值器,开始快,慢慢减速
//            translateY.setInterpolator(new AccelerateDecelerateInterpolator());//默认效果,加速减速插值器,开始快,中间满,结束快
//            translateY.setInterpolator(new AnticipateInterpolator());//反向加速插值器,先后退一段,然后加速
//            translateY.setInterpolator(new OvershootInterpolator());//超越插值器,超越重点一定距离,然后返回
//            translateY.setInterpolator(new CycleInterpolator(2));//循环插值器,在设定的距离内反复移动
//            translateY.setInterpolator(new LinearInterpolator());//线性插值器,匀速直线运动,没有加速减速过程
            translateY.setInterpolator(new BounceInterpolator());//弹球插值器


            translateY.setDuration(4000);
            translateY.setRepeatCount(-1);
            translateY.setRepeatMode(ValueAnimator.RESTART);
            translateY.start();
        });
    }
}